<?php

class Login_model extends CI_Model {

	function validate()
	{
		$this->session->sess_destroy();
		$this->db->where('username', $this->input->post('username'));
		$this->db->where('password', md5($this->input->post('password')));
		$query = $this->db->get('deltager');

		if($query->num_rows == 1)
		{
			return true;
		}

	}

   function get_deltager_data($username) {

	
	$q = $this->db->query("SELECT * FROM deltager WHERE username='".$username."'");

		if($q->num_rows() > 0) {
		
		  foreach($q->result() as $rows) {
			$data = array 
			(
				'id' => $rows->id,
				'navn' => $rows->rollespiller_navn,
				'efternavn' => $rows->rollespiller_efternavn,
				'username' => $rows->username,
				//'password' => $rows->password,
				'email_adress' => $rows->email_adress,
				'type_aktuel' => $rows->type_aktuel,
				'kampagne_aktuel' => $rows->kampagne_aktuel,
				'side_admin' => $rows->side_admin
			);	
		  	
		  }
		return $data;
		}

    }


/*	function find_aktuel_pc($user_id)
	{
		$query = $this->db->query("SELECT blik_aktuel FROM deltager WHERE id='".$user_id."'");
		if($query->num_rows() > 0)	
		{
			$row = $query->row();
			return	$ingame_id['pc_aktuel'] = $row->blik_aktuel;		
		}	

	}
*/

	/** go_ingame
	* Bruges af controlleren login.php - validate_credentials()
	*/
	function go_ingame($ingame_id,$kampagne_aktuel)
	{
		$type_aktuel = $ingame_id;
		## JSON ARRAYS 
		$this->rettighed_model->jSonRettigheder($ingame_id,$kampagne_aktuel);		
		//---SØG-ARRAY---laver et soeg-array i filen './scripts/wysiwyg/resultater.json' til brug for 
		$this->wysiwyg_model->lav_soeg_array();
		$this->load->model('data_model');
		$this->data_model->alle_beskrivelser();
		
		//finder kampagnen
		$query = $this->db->query('
			SELECT kampagne.id, navn.navn, ingame_nu, primaer_gruppe, primaer_begivenhed, primaer_sted, primaer_gruppe_alt, primaer_begivenhed_alt, primaer_sted_alt
			FROM navn, kampagne
			WHERE id_element = '.$kampagne_aktuel.'			
			AND id_element = kampagne.id
		');
		$row = $query->row();	

		//finder gms
		$query2 = $this->db->query('
			SELECT id_gm
			FROM rpg_element_gm
			WHERE id_rpg_element = '.$row->id.'
		');

		$gms = array();
		foreach($query2->result() as $row2){
			$gms[] = $row2->id_gm;
		}

		$data['primaer_sted'] = $row->primaer_sted;
		$data['primaer_gruppe'] = $row->primaer_gruppe;
		$data['primaer_begivenhed'] = $row->primaer_begivenhed;
		$data['primaer_sted_alt'] = $row->primaer_sted_alt;
		$data['primaer_gruppe_alt'] = $row->primaer_gruppe_alt;
		$data['primaer_begivenhed_alt'] = $row->primaer_begivenhed_alt;
		$data['kampagne_id'] = $kampagne_aktuel;  
		$data['kampagne_navn'] = $row->navn;
		$data['ingame_nu'] = $row->ingame_nu;
		$data['gms'] = $gms;

		print_r($this->session->userdata($deltager));

		if((int) $type_aktuel != 0 and (int) $type_aktuel != -1) //hvis man hverken er gm eller side-admin
		{

			$query2 = $this->db->query('
				SELECT navn
				FROM navn
				WHERE id_element = '.$type_aktuel.'
			');
			$row2 = $query2->row();		

			$data['ingame_id']= $type_aktuel;
			$data['ingame_navn'] = $row2->navn;
		
		}
		elseif((int) $type_aktuel == 0) //hvis man er gm
		{
			$deltager = $this->session->userdata('deltager');
		
			$data['ingame_id']= 0 ;
			$data['ingame_navn'] = "Gamemaster";	
		}
		elseif((int) $type_aktuel == -1) //hvis man er side_admin
		{
			$deltager = $this->session->userdata('deltager');

			$data['ingame_id']= -1 ;
			$data['ingame_navn'] = "Sideadministrator";
		}

		$this->session->set_userdata('ingame', $data);	

        $redirect = $this->session->userdata('redirect');
        $this->session->unset_userdata('redirect');


		if(!empty($redirect)){

		   redirect($redirect['url']);
		}

		else{

			redirect("forord");
				/*if($type_aktuel == 0)
	    		    {	redirect("gamemaster/dashboard");}
			    if($type_aktuel == -1)
	    		    {	redirect("admin/dashboard");}
			    else //hvis man er game-master
	    		    {	redirect("deltager/dashboard/".$type_aktuel);}*/
			
		}

	}


}

?>
